package com.huike.web.controller.system;

import com.huike.clues.domain.SysDictType;
import com.huike.clues.domain.vo.SysDictTypeVo;
import com.huike.clues.service.ISysDictTypeService;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysDictTypeDTO;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;



/**
 * @author Ls
 */
@RestController
@Api(tags = "字典类型")
@RequestMapping("/system/dict/type")
@Slf4j
public class SysDictTypeController extends BaseController {

    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 新增字典类型
     */
    @PostMapping
    @ApiOperation("新增字典类型")
    public AjaxResult add(@RequestBody SysDictTypeDTO dto){
        log.info("新增字典类型--前端传来的参数为{}",dto);
        dictTypeService.addType(dto);
        return AjaxResult.success();
    }

    @GetMapping("/{dictId}")
    @ApiOperation("根据id查询字典类型")
    public AjaxResult getById(@PathVariable Long dictId){
        log.info("查询字典类型--前端传来的参数为{}",dictId);
        SysDictType dictType = dictTypeService.getById(dictId);
        return AjaxResult.success(dictType);
    }

    /**
     * 修改字典类型
     */
    @ApiOperation("修改字典类型")
    @PutMapping
    public AjaxResult update(@RequestBody SysDictTypeDTO dto){
        log.info("修改字典类型--前端传来的参数为{}",dto);
        dictTypeService.updateType(dto);
        return AjaxResult.success();
    }

    /**
     * 清空缓存
     */
    @DeleteMapping
    @ApiOperation("清空缓存")
    public AjaxResult clearCache(){
        dictTypeService.clearCache();
        return AjaxResult.success();
    }

    @GetMapping("/export")
    @ApiOperation("导出数据字典类型信息")
    public AjaxResult export(SysDictTypeDTO dto){
        log.info("导出数据字典类型信息--前端传来的参数为{}",dto);
        List<SysDictTypeVo> dictTypeVos = dictTypeService.selectDictTypeList(dto);
        ExcelUtil<SysDictTypeVo> excelUtil = new ExcelUtil<>(SysDictTypeVo.class);
        return excelUtil.exportExcel(dictTypeVos, "字典类型");
    }

    /**
     * 分页查询数据字典类型信息
     */
    @GetMapping("/list")
    @ApiOperation("分页查询数据字典类型信息")
    public TableDataInfo list( SysDictTypeDTO dto){
        log.info("分页查询数据字典类型信息--前端传来的参数为{}",dto);
        startPage();
        List<SysDictTypeVo> list = dictTypeService.selectDictTypeList(dto);
        return getDataTable(list);
    }

    /**
     * 字典类型下拉列表
     * @return 字典类型列表
     */
    @GetMapping("/optionselect")
    @ApiOperation("字典类型下拉列表")
    public AjaxResult optionSelect(){
        log.info("字典类型下拉列表");
        List<SysDictType> list = dictTypeService.optionSelect();
        return AjaxResult.success(list);
    }

    @DeleteMapping("/{dictIds}")
    @ApiOperation("批量删除字典类型")
    public AjaxResult deletByIds(@PathVariable String dictIds){
        log.info("批量删除字典类型--前端传来的参数为{}",dictIds);
        dictTypeService.removeByIds(dictIds);
        return AjaxResult.success();
    }

}
